﻿using System;
using System.Text;
using System.Linq;
using System.Data;
using System.Collections.Generic;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using VIT.Library;

namespace VIT.Web.Admin
{    
    public partial class Permission : VITComponent
    {
        Provider.Account acc; public string roles;
        Function fun;
        protected void Page_Load(object sender, EventArgs e)
        {
            acc = new Provider.Account();
            fun = new Function();
            acc.UserName = Request["usr"];
            
            if (!IsPostBack) LoadData();
        }

        #region Methods
        private void LoadData()
        {
            acc.GetSingle();
            if (!string.IsNullOrEmpty(acc.UserName))
            {
                // kiểm tra user cần sửa coa phải thuộc tập con của user hiện tại
                string[] arrChild = acc.GetAll(ChildId: "Administrator").GetAllChildId(Session["userName"].ToString(), "CreateByUser", "UserName").Split(',');
                List<string> lstChild = arrChild.ToList();

                if (lstChild.Contains(acc.UserName))
                {
                    StringBuilder str = new StringBuilder();
                    roles = acc.GetRoles();

                    foreach (string p in fun.getDistinctPage())
                    {
                        DataTable table = acc.getRolesByPage(Session["userName"].ToString(), p);
                        if (table.Rows.Count > 0)
                        {
                            str.Append("<fieldset>");
                            str.Append("<legend>");
                            str.Append(p);
                            str.Append("</legend>");
                            str.Append("<table class='nostyle' style='float:left'>");
                            str.Append("<thead><tr>");
                            str.Append("<th width='2%'></th>");
                            str.Append("<th>Chức năng</th>");
                            str.Append("<th>Mã</th>");
                            str.Append("</tr></thead>");
                            for (int i = 0; i < table.Rows.Count; i++)
                            {
                                str.Append("<tr class='row");
                                str.Append(i % 2);
                                str.Append("'>");
                                str.Append("<td align='center'><input type='checkbox' name='role' value='");
                                str.Append(table.Rows[i]["Name"]);
                                str.Append("' ");
                                if (roles.Contains(table.Rows[i]["Name"].ToString())) str.Append(" checked='checked'");
                                str.Append("/></td>");
                                str.Append("<td>");
                                str.Append(table.Rows[i]["Description"]);
                                str.Append("</td>");
                                str.Append("<td>");
                                str.Append(table.Rows[i]["Name"]);
                                str.Append("</td>");
                                str.Append("</tr>");
                            }
                            str.Append("</table>");
                            str.Append("</fieldset>");
                        }
                    }

                    //if (acc.UserName != "")
                    //{
                    //    ListView.DataSource = acc.GetAllRoles(Session["userName"].ToString());
                    //    ListView.DataBind();
                    //}

                    divXuat.InnerHtml = str.ToString();
                }
                else
                {
                    RedirectComponent("Account", "AccountList");
                }
            }
            else
            {
                RedirectComponent("Account", "AccountList");
            }
        }

        private void SaveData()
        {
            acc.Roles = Request["role"].Replace(',', '|');
            if (acc.SetRoles()) setTrueVisible(lblThanhCong);
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;

            control.Visible = true;
        }
        #endregion

        #region Events
        protected void Save_Click(object sender, EventArgs e)
        {
            SaveData();
            LoadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Account", "AccountList");
        }
        #endregion
    }
}